import { Viewer } from 'cesium';
import * as Cesium from 'cesium';

interface PipeLine {
  url: string;
  layers: string;
  name: string;
}

export const useDrawPipeLine = () => {
  const layerGroups = {
    '高压管线': {},
    '次高压管线': {},
    '中压管线': {},
    '低压管线': {}
  };
  const drawPipeline = async (option: PipeLine) => {
    if (!window.viewer || !window.viewer.imageryLayers) {
      console.warn('Cesium viewer is not available, cannot draw pipeline');
      return;
    }
    const esri = await Cesium.ArcGisMapServerImageryProvider.fromUrl(option.url, {
      layers: option.layers
    });
    layerGroups[option.name] = window.viewer.imageryLayers.addImageryProvider(esri);
  };
  const removePipeline = (name: string) => {
    if (!window.viewer || !window.viewer.imageryLayers) {
      console.warn('Cesium viewer is not available, cannot remove pipeline');
      return;
    }
    if (layerGroups[name]) {
      window.viewer.imageryLayers.remove(layerGroups[name]);
      delete layerGroups[name];
    }
  };
  return {
    drawPipeline,
    removePipeline
  };
};
